import {
  getProductList,
  getCustomerList
} from "@src/api/productRegisterApi";
import saveToCustomer from '../component/saveToCustomer.vue';
import saveToProduct from '../component/saveToProduct.vue';
import { openAccurateTab } from '@src/util/platform';
import { PageRoutesTypeEnum } from 'pub-bbx-global/pageType/dist/enum/PageRoutesEnum'
export default {
  data() {
    return {
      saveToData: {
        id: '',
        customer: {
          visibleProp: false,
          saveLoading: false,
          saveToTotal: 0
        },
        product: {
          visibleProp: false,
          saveLoading: false,
          saveToTotal: 0
        },
      }
    }
  },
  methods: {
    saveTo(type, id, row) {
      this.checkList(type, id, row)
    },
    // 确认是否有数据
    checkList(type, id, row) {
      let fn
      if (type === 'product') {
        fn = getProductList
      } else {
        fn = getCustomerList
      }
      let flag
      if (row) {
        this.$set(row, `${type}SaveLoading`, true)
      } else {
        this.saveToData[type].saveLoading = true
      }
      fn({
        id,
        pageNum: 1,
        pageSize: 1
      }).then(res => {
        flag = !!res.data?.list?.length
        this.saveToData[type].saveToTotal = res.data?.total || 0
      }).catch(err => {
        console.log(err)
        flag = false
      }).finally(() => {
        this.saveToData.id = id
        flag !== void 0 && this.ifOpenSaveToDialog(type, flag)

        if (row) {
          this.$set(row, `${type}SaveLoading`, false)
        } else {
          this.saveToData[type].saveLoading = false
        }
      })
    },
    goToProdcutCreate () {
      window.TDAPP?.onEvent('pc：产品管理-新建事件');
      // window.location = '/customer/product/create';
      let fromId = window.frameElement.getAttribute('id');

      // this.$platform.openTab({
      //   id: 'customer_product_create',
      //   title: '保存至产品',
      //   url: `/customer/product/register/create/${this.saveToData.id}`,
      //   reload: true,
      //   close: true,
      //   fromId,
      // });
      openAccurateTab({
        type: PageRoutesTypeEnum.PageCustomerProductRegisterCreate,
        key: this.saveToData.id,
        reload: true,
        fromId
      })
    },
    jumpPage() {
      window.TDAPP?.onEvent('pc：客户管理-新建事件');

      // window.location = '/customer/create';
      let fromId = window.frameElement.getAttribute('id');

      // this.$platform.openTab({
      //   id: 'customer_create',
      //   title: '保存至客户',
      //   url: `/customer/register/create/${this.saveToData.id}`,
      //   reload: true,
      //   close: true,
      //   fromId
      // });
      openAccurateTab({
        type: PageRoutesTypeEnum.PageCustomerRegisterCreate,
        key: this.saveToData.id,
        reload: true,
        fromId
      })
    },
    // 打开弹框或者跳转
    ifOpenSaveToDialog(type, flag) {
      if(flag) {
        this.saveToData[type].visibleProp = true
      } else {
        if(type == 'product'){
          this.goToProdcutCreate()
        }else{
            this.jumpPage()
        }
        // 跳转对应页面
      }
    },
    saveToP(item){
      window.TDAPP?.onEvent('pc：产品管理-编辑事件');
      // window.location = '/customer/product/create';
      let fromId = window.frameElement.getAttribute('id');
      this.saveToData.product.visibleProp = false
      // this.$platform.openTab({
      //   id: 'customer_product_edit',
      //   title: '编辑产品',
      //   url: `/customer/product/register/edit/${item.chooseData.id}/${this.saveToData.id}`,
      //   reload: true,
      //   close: true,
      //   fromId,
      // });
      openAccurateTab({
        type: PageRoutesTypeEnum.PageCustomerProductRegisterEdit,
        key: `${item.chooseData.id}/${this.saveToData.id}`,
        reload: true,
        fromId
      })

      // window.location.href = `/customer/product/register/edit/${item.id}/${this.saveToData.id}`;
    },
    saveToC(item){
      window.TDAPP?.onEvent('pc：客户管理-编辑事件');
      // window.location = '/customer/product/create';
      let fromId = window.frameElement.getAttribute('id');
      this.saveToData.customer.visibleProp = false
      // this.$platform.openTab({
      //   id: 'customer_edit',
      //   title: '编辑客户',
      //   url: `/customer/register/edit/${item.chooseData.id}/${this.saveToData.id}`,
      //   reload: true,
      //   close: true,
      //   fromId,
      // });
      openAccurateTab({
        type: PageRoutesTypeEnum.PageCustomerRegisterEdit,
        key: `${item.chooseData.id}/${this.saveToData.id}`,
        reload: true,
        fromId
      })
    },
  },
  components: {
    saveToCustomer,
    saveToProduct
  }
}